
// 天气查询接口：
//      https://www.yiketianqi.com/free/week?unescape=1&appid=99217431&appsecret=9MyKLYzD'; 支持城市名查询， 范围限国内
//      百度地图web api; 只支持城市编码号查询; 尚未实现城市名转为城市编码号

// 此接口只实现了国内天气查询
class Weather{
    constructor(){
        this.city = '';
        this.cityCode = '';
        this.weather_week = '';
        this.windV = [];
        this.windA = [];
        this.levelToSpeed = {
            // 风力等级:风速(m/s)
            0: 0.1,
            1: 0.9,
            2: 2.4,
            3: 4.4,
            4: 6.7,
            5: 9.8,
            6: 12.8,
            7: 15.5,
            8: 19.0,
            9: 22.6,
            10: 26.5,
            11: 30.5,
            12: 34.9, 
        }
    }

    init(){
        this.city = '';
        this.cityCode = '';
        this.weather_week = '';
        this.windV = [];
        this.windA = [];
    }

    // 从百度地图api中获得爆炸地点所属城市
    getLocalWeather(point, day){
        let geocoder = new BMap.Geocoder();
        let that = this;
        geocoder.getLocation(point, function(result){
            that.city = result.addressComponents.city;
            if(that.city.lastIndexOf('市') != -1)
                that.city = that.city.slice(0, that.city.lastIndexOf('市'));
            that.getWeather(day)
        });
    }

    getWeather(day){
        let url = 'https://www.yiketianqi.com/free/week?unescape=1&appid=99217431&appsecret=9MyKLYzD' + '&city=' + this.city;
        if(debug) console.log('getweather url ', url);
        var xmlhttp;
        if (window.XMLHttpRequest) {
            // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp = new XMLHttpRequest();
        }
        else {
            // IE6, IE5 浏览器执行代码
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        let that = this;
        xmlhttp.onreadystatechange = () => {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                console.log(JSON.parse(xmlhttp.responseText));
                if(JSON.parse(xmlhttp.responseText).city != this.city){
                    alert('风速风向获取失败。可能的原因是爆炸地点处于国外，无法查询；或者接口达到每日使用次数');
                    return false;
                }
                //获取所在城市的对应城市编码
                that.weather_week = JSON.parse(xmlhttp.responseText).data;
                if(debug)console.log('weather_week', that.weather_week);
                that.getWind();
                console.log('windV[0], windA[0]',that.windV[0], that.windA[0]);
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }

    getWind(){
        for(let i = 0; i < this.weather_week.length; i++){
            let windV_info = this.weather_week[i].win_speed;
            let windA_info = this.weather_week[i].win;
            switch(windA_info){
                case '东风':
                    this.windA.push(- 180);
                    break;
                case '南风':
                    this.windA.push(90);
                    break;
                case '西风':
                    this.windA.push(0);
                    break;
                case '北风':
                    this.windA.push(90);
                    break;
                case '东南风':
                    this.windA.push(135);
                    break;
                case '东北风':
                    this.windA.push(-135);
                break;
                case '西南风':
                    this.windA.push(45);
                    break;
                case '西北风':
                    this.windA.push(45);
                    break; 
            }
            let windlevels = [];
            let winds = windV_info.split('转');
            for(let i = 0; i < winds.length; i++){
                windlevels.push(this.windinfoToSpeed(winds[i]));
            }
            let sum = 0;
            for(let i = 0; i < windlevels.length; i++){
                sum += windlevels[i];
            }
            this.windV.push((sum/windlevels.length).toFixed(2));
        }
    }

    windinfoToSpeed(windinfo){
        if(windinfo.lastIndexOf('<') != -1){
            let level = (parseFloat(windinfo[windinfo.lastIndexOf('<')+1])/2).toFixed(0);
            // console.log('level, levelToSpeed[level]', level, this.levelToSpeed[level])
            return this.levelToSpeed[level];
        }
        else if(windinfo.lastIndexOf('-') != -1){
            let level = windinfo.split(/[-]/)[0];
            // console.log('level, levelToSpeed[level]', level, this.levelToSpeed[level])
            return this.levelToSpeed[level];
        }
    }
}